<template>
  <div class="login-page">
    <div class="login-top">
      <div class="logo-con">
        <!-- <img src="@images/login/login-logo.svg" /> -->
        <!-- <img class="logo-img" src="@/assets/images/login/logo.png" /> -->
        <div class="name">中小微企业判定监测系统</div>
      </div>
      <!-- <div class="btn switch" @click="showLoginBox = !showLoginBox">{{ showLoginBox ? '隐藏登录' : '显示登录' }}</div> -->
    </div>
    <div class="content">
      <div class="login-box" v-show="showLoginBox">
        <!-- <i class="el-icon-close" @click="showLoginBox = !showLoginBox"></i> -->
        <div class="logo" :class="{ 'login-logo': formType === 'login' }">
          <!-- <img src="@images/login/login-logo.svg" alt="" /> -->
          <div class="form-title">欢迎使用中小微企业判定监测系统</div>
        </div>
        <!-- <ul v-if="formType === 'login'" :class="['login-type', loginType]">
          <li
            :class="['login-item', 'login-type-1', loginType === 'account' ? 'active' : '']"
            @click.stop="onLoginTypeChange('account')">
            账号登录
          </li>
        </ul> -->
        <template v-if="formType === 'login'">
          <QuickForm v-if="loginType === 'quick'" @login="onLogin"></QuickForm>
          <AccountForm v-else @login="onAccountLogin"></AccountForm>
        </template>
        <RegisterForm v-else @register="onRegister"></RegisterForm>
        <!--  <div class="login-bottom-btn">
          <span v-if="formType !== 'login'">已有账号？</span>
          <el-button type="text" @click.stop="toggleForm">
            {{ formType === 'login' ? '立即注册' : '立即登录' }}
          </el-button> -->
        <!-- <el-button v-if="loginType === 'account' && formType === 'login'" type="text" @click.stop="showForgetDialog">
            忘记密码？
          </el-button>
        </div> -->
        <!-- <img class="login-img" src="../../assets/images/login/login-img.svg" /> -->
      </div>
    </div>
    <forget-pwd-dialog :visible.sync="forgetDialogVisible"></forget-pwd-dialog>
  </div>
</template>

<script>
import { mapActions, mapState } from 'vuex'
import { isMobile } from '@utility/utility'
import { Session, Local } from '@utility/storage.js'
import ForgetPwdDialog from './components/ForgetPwdDialog.vue'
import QuickForm from './components/QuickForm.vue'
import AccountForm from './components/AccountForm.vue'
import RegisterForm from './components/RegisterForm.vue'
// import VersionLabel from '../../version/VersionLabel.vue'

import { loginApiPass } from '@apis/login.js'

export default {
  name: 'LoginPage',
  components: {
    ForgetPwdDialog,
    QuickForm,
    AccountForm,
    RegisterForm,
    // VersionLabel,
  },
  data() {
    return {
      // 登录角色：管理员-admin，普通用户-common
      loginRole: 'admin',
      forgetDialogVisible: false,
      formType: 'login', // 表单类型，login-登录表单，register-注册表单
      loginType: 'account',
      showLoginBox: true,
    }
  },
  created() {
    if (isMobile()) {
      this.loginConfirm()
    }
  },
  computed: {
    ...mapState(['userPermissions']),
  },
  methods: {
    ...mapActions('user', ['setUserInfos']),
    loginConfirm() {
      if (isMobile()) {
        this.$confirm('暂不支持移动端登录，请移步到 PC 端登录', '提示', {
          confirmButtonText: '确定',
          showCancelButton: false,
          customClass: 'plat-msg-box',
          type: 'warning',
        })
          .then(() => {
            return false
          })
          .catch(() => {
            return false
          })
      } else {
        return true
      }
    },
    onLoginTypeChange(type) {
      this.loginType = type
    },
    async onAccountLogin(obj) {
      const params = {
        loginName: obj.loginName || '',
        loginPassword: obj.loginPassword || '',
        smCode: '',
      }
      loginApiPass(params).then(res => {
        setTimeout(() => {
          this.loginSuccess(res || {})
        }, 500)
      })
    },
    // 登录成功回调
    async loginSuccess(data) {
      // 本地免登陆验证
      Local.set('token', data.access_token)
      Session.set('token', data.access_token)
      Session.set('refresh_token', data.refresh_token)

      try {
        // 登录成功后获取用户信息，存储到vuex
        await this.setUserInfos()
        // 获取角色菜单，存储到vuex
        await this.$store.dispatch('getUserPermissions')
      } catch (err) {
      } finally {
        if (this.userPermissions && this.userPermissions.length) {
          this.$router.push({
            name: this.userPermissions[0].belongPage,
          })
        }
      }
    },
    // 打开弹窗
    showForgetDialog() {
      if (isMobile()) {
        this.loginConfirm()
      } else {
        this.forgetDialogVisible = true
      }
    },
    // 切换表单
    toggleForm() {
      this.formType = this.formType === 'login' ? 'register' : 'login'
      if (this.formType === 'login') {
        this.loginType = 'account'
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.login-page {
  position: relative;
  width: 100%;
  height: 100%;
  color: #333;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  background: url('../../assets/images/login/bg.jpg') no-repeat;
  background-size: 100% 100%;
  font-family: 'syht-Regular';
  .content {
    width: 100%;
    // height: 100%;
    flex-shrink: 0;
    flex: 1;
    // background-position: right 0 top -120px;
    position: relative;
  }
  .login-top {
    height: 70px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    // background-color: #3b93f7;
    padding: 0 30px;
    position: sticky;
    top: 0;
    z-index: 20;
    .logo-con {
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 28px;
      font-weight: 600;
      color: #fff;
      .logo-img {
        width: 40px;
        height: 40px;
        object-fit: contain;
        margin-right: 15px;
      }
      .name {
        letter-spacing: 3px;
        // margin-left: 10px;
      }
    }
    .btn {
      height: 40px;
      width: 120px;
      display: flex;
      justify-content: center;
      align-items: center;
      color: #3b93f7;
      font-size: 16px;
      font-weight: 600;
      background: #fff;
      border-radius: 40px;
      cursor: pointer;
    }
  }

  .login-box {
    position: absolute;
    z-index: 1;
    right: 20%;
    top: 50%;
    transform: translate(0, -50%);
    width: 480px;
    // padding: 50px 30px 30px;
    padding: 50px 30px 70px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    box-shadow: 0px 0px 10px 0px rgba(2, 2, 2, 0.22);
    background: #fff;
    // &.register-login-box {
    //   top: 0;
    //   transform: translateY(0);
    // }
    .el-icon-close {
      position: absolute;
      top: 15px;
      right: 20px;
      font-size: 26px;
      cursor: pointer;
    }
    .form-title {
      font-size: 26px;
      letter-spacing: 2px;
      color: rgb(33, 41, 62);
      font-weight: 400;
      text-align: center;
      margin-bottom: 45px;
      margin-top: 15px;
      font-family: 黑体, sans-serif;
    }
    .register-tips-txt {
      margin-bottom: 10px;
      font-size: 15px;
      font-weight: 600;
      color: #666;
      text-align: center;
      strong {
        font-size: 24px;
        padding: 0 4px;
        color: $-color-danger;
      }
    }
    .login-type {
      width: 100%;
      max-width: 360px;
      margin: 0 auto;
      margin-bottom: 20px;
      font-size: 0;
      position: relative;
      text-align: center;
      .login-item {
        display: inline-block;
        cursor: pointer;
        color: #707070;
        font-weight: 400;
        vertical-align: top;
        font-size: 20px;
        height: 26px;
        line-height: 26px;
        &.login-type-1 {
          text-align: center;
          // padding-right: 20px;
        }
        &.login-type-2 {
          text-align: center;
          // padding-left: 20px;
        }
      }
      .login-item.active {
        color: #000;
      }
      &::after {
        display: block;
        content: '';
        height: 2px;
        bottom: -5px;
        position: absolute;
        background-color: #006ff2;
      }
      &.account::after {
        width: 80px;
        left: calc(50% - 40px);
        transition: left 0.3s;
      }
      &.quick::after {
        width: 100px;
        left: calc(50% + 10px);
      }
    }
    .logo {
      position: relative;
      width: 100%;
      text-align: center;
      // margin-bottom: 30px;
      img {
        display: inline-block;
        width: 100%;
        margin-bottom: 30px;
      }
      .title {
        font-size: 36px;
      }
    }
    .login-bottom-btn {
      margin-top: 10px;
      font-size: 13px;
      text-align: center;
      .el-button {
        font-size: 13px;
      }
    }

    .service-hotline-box {
      margin-top: 20px;
      &.register-hotline-box {
        margin-top: 0;
      }
      .tips {
        font-size: 13px;
        color: #999;
        text-align: center;
        line-height: 1.5;
        a {
          color: #999;
        }
      }
    }
  }
  .login-foot {
    background: #abd1ec;
    color: #fff;
    position: relative;
    padding: 30px 10%;
    .foot-list {
      text-align: center;
      margin-bottom: 10px;
      .link {
        color: #fff;
        &:not(:first-of-type) {
          margin-left: 30px;
        }
      }
    }
  }
  .version-info {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    text-align: center;
    padding: 10px;
  }
}
</style>
<style lang="scss">
.plat-msg-box {
  &.el-message-box {
    width: 100%;
    min-width: 360px;
    max-width: 480px;
  }
}
</style>
